﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using publico;
using negocios;
using objetos;
using System.Text;
using System.Data;

namespace softsales.pagina.nf_historico
{
    public partial class nf_historico_modal : System.Web.UI.Page
    {
        BLL_TB_NF_HISTORICO b_tb_nf_historico = new BLL_TB_NF_HISTORICO();
        OBJ_TB_NF_HISTORICO o_tb_nf_historico = new OBJ_TB_NF_HISTORICO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (PUB_SESSAO.Logado())
            {
                if (!IsPostBack)
                {
 

                    PUB_PAGINACAO.Inicializa();

                    if (Request["id_nf"] != null)
                    {
                        HD_ID_NF.Value = Request["id_nf"].ToString();
                    }

                    CarregaGrid();

                }

            }
        }

        protected void IMG_BTN_PESQUISAR_Click(object sender, ImageClickEventArgs e)
        {
            
        }

        public void CarregaGrid()
        {

            try
            {
                StringBuilder sb = new StringBuilder();
                DataSet ds = new DataSet();

                List<string> lista = new List<string>();

                
                if (HD_ID_NF.Value != string.Empty)
                {
                    sb.Append(string.Format(" id_nf = {0}", HD_ID_NF.Value));
                }
              

                ds = b_tb_nf_historico.Selecionar(sb.ToString(), PUB_PAGINACAO.Sort, PUB_PAGINACAO.PageIndex, PUB_PAGINACAO.PageSize);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    GRV_TB_NF_HISTORICO.DataSource = ds;
                    GRV_TB_NF_HISTORICO.DataBind();
                    PUB_PAGINACAO.Paginacao(true);
                }
                else
                {
                    GRV_TB_NF_HISTORICO.EmptyDataText = "Nenhum registro foi encontrado";
                    GRV_TB_NF_HISTORICO.DataBind();
                }
            }
            catch (Exception)
            {
                              
            }


        }

        protected void GRV_TB_NF_HISTORICO_Sorting(object sender, GridViewSortEventArgs e)
        {

            PUB_PAGINACAO.Sort = e.SortExpression;
            if (PUB_PAGINACAO.SortDirection == SortDirection.Ascending)
            {
                PUB_PAGINACAO.SortDirection = SortDirection.Descending;
                PUB_PAGINACAO.Sort = PUB_PAGINACAO.Sort + " " + PUB_PAGINACAO.DESCENDENTE;
            }
            else
            {
                PUB_PAGINACAO.SortDirection = SortDirection.Ascending;
                PUB_PAGINACAO.Sort = PUB_PAGINACAO.Sort + "  " + PUB_PAGINACAO.ASCENDENTE;
            }

            CarregaGrid();
        }


        protected void GRV_TB_NF_HISTORICO_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GRV_TB_NF_HISTORICO.PageIndex = e.NewPageIndex;
            PUB_PAGINACAO.PageIndex = e.NewPageIndex;
            CarregaGrid();
        }

 

        protected void IMG_BTN_PRIMEIRO_Click(object sender, ImageClickEventArgs e)
        {
            PUB_PAGINACAO.PageIndex = 0;
            CarregaGrid();
        }

        protected void IMG_BTN_ANTERIOR_Click(object sender, ImageClickEventArgs e)
        {
            PUB_PAGINACAO.PageIndex = PUB_PAGINACAO.PageIndex - 1;
            CarregaGrid();
        }

        protected void IMG_BTN_PROXIMO_Click(object sender, ImageClickEventArgs e)
        {
            PUB_PAGINACAO.PageIndex = PUB_PAGINACAO.PageIndex + 1;
            CarregaGrid();
        }

        protected void IMG_BTN_ULTIMO_Click(object sender, ImageClickEventArgs e)
        {
            PUB_PAGINACAO.PageIndex = PUB_PAGINACAO.TotalDePaginas - 1;
            CarregaGrid();
        }

    }
}